package com.fourforfo.fourmall.coupon.service.impl;

import com.fourforfo.common.to.MemberPrice;
import com.fourforfo.common.to.SkuReductionTO;
import com.fourforfo.fourmall.coupon.entity.MemberPriceEntity;
import com.fourforfo.fourmall.coupon.entity.SkuLadderEntity;
import com.fourforfo.fourmall.coupon.service.MemberPriceService;
import com.fourforfo.fourmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;

import com.fourforfo.fourmall.coupon.dao.SkuFullReductionDao;
import com.fourforfo.fourmall.coupon.entity.SkuFullReductionEntity;
import com.fourforfo.fourmall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存商品发布时附带的sku优惠信息
     * sku的优惠、满减等信息 fourmall_sms-->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
     * @param skuReductionTO
     * @return
     */
    @Override
    public void saveSkuReduction(SkuReductionTO skuReductionTO) {
        //1.保存满减打折，会员价 sms_sku_ladder
        if(skuReductionTO.getFullCount()>0){
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTO,skuLadderEntity);//sku_id,fullCount,discount
            skuLadderEntity.setAddOther(skuReductionTO.getCountStatus());//是否叠加其他优惠
            skuLadderService.save(skuLadderEntity);
        }


        //2. sms_sku_full_reduction
        if(new BigDecimal("0").compareTo(skuReductionTO.getFullPrice())==-1){//即当fullPrice>0时
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTO,skuFullReductionEntity);
            this.save(skuFullReductionEntity);
        }


        //3. sms_member_price
        List<MemberPrice> memberPrices = skuReductionTO.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrices.stream().
                filter(memberPrice -> new BigDecimal("0").compareTo(memberPrice.getPrice())==-1).
                map(memberPrice -> {
            MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
            memberPriceEntity.setSkuId(skuReductionTO.getSkuId());
            memberPriceEntity.setMemberLevelId(memberPrice.getId());
            memberPriceEntity.setMemberLevelName(memberPrice.getName());
            memberPriceEntity.setMemberPrice(memberPrice.getPrice());
            memberPriceEntity.setAddOther(1);//设置默认叠加伤害
            return memberPriceEntity;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}